/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mall.db.entities;

/**
 *
 * @author Iaroslav_Mazai
 */
public abstract class EntityWithId {

	public abstract Integer getId();

	// For mocking purposes only
	protected abstract void setId(Integer newId);

	@Override
	public int hashCode() {
		if (getId() != null) {
			return 21 * getId();
		} else {
			return 0;
		}
	}

	@Override
	public boolean equals(Object object) {
		if (object == null) {
			return false;
		}

		if (this == object) {
			return true;
		}

		if (this.getId() == null) {
			return false;
		}

		if (!this.getClass().equals(object.getClass())) {
			return false;
		}

		EntityWithId casted = (EntityWithId) object;
		if (casted.getId() == null) {
			return false;
		}
		if (casted.getId().equals(this.getId())) {
			return true;
		}

		return false;
	}
}
